<template>
  <div class="login">
    <div class="login_context">
      <div>
        <img src="..\assets\logo.png" alt="" />
      </div>
      <router-view/>
      <el-form :model="loginForm" ref="loginFormRef">
        <el-form-item label="用户名" prop="user_name">
          <el-input placeholder="请输入老师的用户名" v-model="loginForm.user_name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="user_password">
          <el-input
            type="password"
            v-model="loginForm.user_password"
            placeholder="请输入老师的密码" 
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info" @click="resetLoginForm">重置</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //表单请求数据
      loginForm: {
        user_name: "",
        user_password: ""
      },
      
    };
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      let self=this;
      this.$axios.post('/api/teacher/login',this.$qs.stringify({user_name:self.loginForm.user_name,user_password:self.loginForm.user_password}))
        .then(function(res){
          if (res.data === "")  self.$message.error("登录失败");
          else {
            self.$message.success("登录成功");
            let teacher=res.data;
            sessionStorage.setItem('teacher',JSON.stringify(teacher));
            self.router.push({path:"/teacherIndex",query:{teacher_name:res.data.teacher_name}});
          }
        })
        .catch((error) => {
         console.log(error);
        });
    }
    

  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
* {
  padding: 0;
  margin: 0;
}
.login {
  background-image: url('../assets/上海海洋大学.png') ;
  background-size:1600px 800px;
  height: 100vh;
}
.login_context {
  width: 400px;
  height: 370px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  background-color: white;
  box-shadow: 0 0 10px 2px #ddd;
}
.login_context img {
  width: 400px;
  height: 100px;
}
.login_context form {
  margin-left: 40px;
}
.login_context form input {
  width: 300px;
}
</style>
